package com.beardedcoder.lowPower.activities;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

import com.beardedcoder.lowPower.R;
import com.beardedcoder.utils.ui.ActionBar;

public class ActivityCell extends Activity {
	ToggleButton mEnable;
	TelephonyManager mTelephoneManager;
	ActionBar mActionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_state_cell);
		loadViews();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		setVariables();
		refreshViews();
	}

	private void setVariables() {
		mTelephoneManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
	}

	private void loadViews() {
		mEnable = (ToggleButton) findViewById(R.id.activity_state_cell_data_enable);
		mActionbar = (ActionBar) findViewById(R.id.actionbar);
		ActionBar.ActionText title = new ActionBar.ActionText(this) {
			public void actionClicked() { }
		};
		title.setText("Cell State");
		mActionbar.addAction(title, ActionBar.CONTAINER_TITLE);
		
		OnCheckedChangeListener listener = new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					adjustCellDataState(isChecked);
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		};
		mEnable.setOnCheckedChangeListener(listener);
	}
	
	private void refreshViews() {
		//mEnable.setChecked(mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED || mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING);
	}
	
	private void adjustCellDataStateB (boolean enable) {
		
	}
	
	/**
	 * 
	 * @param enabled
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private void adjustCellDataState(boolean enabled) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Method dataConnSwitchmethod;
	    Class telephonyManagerClass;
	    Object ITelephonyStub;
	    Class ITelephonyClass;

	    boolean isEnabled = false;
	    isEnabled = mTelephoneManager.getDataState() == TelephonyManager.DATA_CONNECTED;

	    telephonyManagerClass = Class.forName(mTelephoneManager.getClass().getName());
	    Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
	    getITelephonyMethod.setAccessible(true);
	    ITelephonyStub = getITelephonyMethod.invoke(mTelephoneManager);
	    ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

	    if (isEnabled) {
	        dataConnSwitchmethod = ITelephonyClass
	                .getDeclaredMethod("disableDataConnectivity");
	    } else {
	        dataConnSwitchmethod = ITelephonyClass
	                .getDeclaredMethod("enableDataConnectivity");   
	    }
	    dataConnSwitchmethod.setAccessible(true);
	    dataConnSwitchmethod.invoke(ITelephonyStub);
	}	
}
